/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package catalog;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * This class stores a single CD and its state
 * @author smoniz
 *
 */
public class CD {

    String genre, artist, title;

    public String getArtist() {
        return artist;
    }

    public String getGenre() {
        return genre;
    }

    public String getTitle() {
        return title;
    }

    public CD(String g, String a, String t) {
        genre = g;
        artist = a;
        title = t;

    }

    /**
     * Return a cd from a document element
     * @param e DOM element
     * @return
     */
    public static CD fromXMLElement(Element e) {
        return new CD(getTextValue(e, "genre"), getTextValue(e, "artist"), getTextValue(e, "title"));
    }

    /**
     * Returns a string from the element and a tag
     * from http://www.java-samples.com/showtutorial.php?tutorialid=152
     * @param ele
     * @param tagName
     * @return
     */
    public static String getTextValue(Element ele, String tagName) {
        String textVal = null;
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            textVal = el.getFirstChild().getNodeValue();
        }

        return textVal;
    }

    public String toString() {
        return "<html>" + genre + ":" + artist + ":" + title + "</html>";
    }
}

